/**
 * Copyright 2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * ChunMap是开源软件。你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。
 * 
 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 * 
 * 下面此段程序作者YangJianDong(chunquedong)
 * 最后修改时间2010.7.17
 */
package chunmap.data.provider;

import chunmap.data.feature.FeatureCollection;
import chunmap.data.feature.ShapeFeature;
import chunmap.data.index.FeatureList;
import chunmap.data.index.QuartTree;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.Geometry;

public class GeoDataAdapter {
	//过滤用的事件
    public interface GeometryFilterEventHandler{
    	public boolean apply(Envelope envelop);
    }
    public GeometryFilterEventHandler geometryFilter;

    FeatureDataReader dataResources;

    public GeoDataAdapter(FeatureDataReader dataResources)
    {
        this.dataResources = dataResources;
    }
    //------------------------------------------------------------------------
    private void initFeatureCollection(FeatureCollection featureClass)
    {
        for (long i = 0, n = dataResources.size(); i < n; i++)
        {
            Envelope envelop = dataResources.getShapeEnvelop(i);

            if (geometryFilter == null || geometryFilter.apply(envelop))
            {
                Geometry geo=dataResources.getGeometry(i);
                if (geo == null)
                {
                    continue;
                }
                ShapeFeature shape = new ShapeFeature();
                shape.setGeometry(geo);
                shape.setId(i);

                featureClass.insert(shape);
                shape.setFeatureClass(featureClass);
            }
        }
        featureClass.setDataResources(dataResources);
        featureClass.setEnvelope(dataResources.getEnvelop());
        featureClass.setMetadata(dataResources.getMetadata());
    }

    public FeatureCollection createFeatureList()
    {
    	FeatureList featureClass = new FeatureList();
        initFeatureCollection(featureClass);
        return featureClass;
    }
    public FeatureCollection createQuartTree()
    {
        Envelope allEnvelope = dataResources.getEnvelop();
        FeatureCollection featureClass = new QuartTree(allEnvelope);
        initFeatureCollection(featureClass);
        return featureClass;
    }
}
